home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / hash / md5.doc < prev    next >
Text File  |  1993-07-23  |  42KB  |  1,279 lines

  1.  
  2.  
  3.  
  4.  
  5. Network Working Group                                          R. Rivest
  6. INTERNET-DRAFT                       MIT Laboratory for Computer Science
  7.                                                                 S. Dusse
  8.                                                  RSA Data Security, Inc.
  9.                                                             10 July 1991
  10.  
  11.  
  12.                     The MD5 Message-Digest Algorithm
  13.  
  14.  
  15. STATUS OF THIS MEMO
  16.  
  17.    This draft document will be submitted to the RFC editor as a protocol
  18.    specification. Comments should be sent to <pem-dev@tis.com> or to the
  19.    authors. Distribution of this memo is unlimited.
  20.  
  21.  
  22. ACKNOWLEDGEMENT
  23.  
  24.    We would like to thank Don Coppersmith, Burt Kaliski, Ralph Merkle,
  25.    David Chaum, and Noam Nisan for numerous helpful comments and
  26.    suggestions.
  27.  
  28.  
  29. Table of Contents
  30.  
  31.    1. Executive Summary                                                1
  32.    2. Terminology and Notation                                         2
  33.    3. MD5 Algorithm Description                                        3
  34.    4. Summary                                                          7
  35.    5. Summary of Differences Between MD4 and MD5                       7
  36.    6. Security Considerations                                          7
  37.    References                                                          8
  38.    Authors' Addresses                                                  8
  39.    APPENDIX - Reference Implementation                                 9
  40.  
  41.  
  42.  
  43. 1. Executive Summary
  44.  
  45.    This document describes the MD5 message-digest algorithm. The
  46.    algorithm takes as input an input message of arbitrary length and
  47.    produces as output a 128-bit "fingerprint" or "message digest" of the
  48.    input. It is conjectured that it is computationally infeasible to
  49.    produce two messages having the same message digest, or to produce
  50.    any message having a given prespecified target message digest. The
  51.    MD5 algorithm is intended for digital signature applications, where a
  52.    large file must be "compressed" in a secure manner before being
  53.    encrypted with a private (secret) key under a public-key cryptosystem
  54.    such as RSA.
  55.  
  56.  
  57.  
  58.  
  59. Rivest and Dusse                                                [Page 1]
  60. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  61.  
  62.  
  63.    The MD5 algorithm is designed to be quite fast on 32-bit machines. In
  64.    addition, the MD5 algorithm does not require any large substitution
  65.    tables; the algorithm can be coded quite compactly.
  66.  
  67.    The MD5 algorithm is an extension of the MD4 message digest algorithm
  68.    [1,2]. MD5 is slightly slower than MD4, but is more "conservative" in
  69.    design. MD5 was designed because it was felt that MD4 was perhaps
  70.    being adopted for use more quickly than justified by the existing
  71.    critical review; because MD4 was designed to be exceptionally fast,
  72.    it is "at the edge" in terms of risking successful cryptanalytic
  73.    attack. MD5 backs off a bit, giving up a little in speed for a much
  74.    greater likelihood of ultimate security. It incorporates some
  75.    suggestions made by various reviewers, and contains additional
  76.    optimizations.
  77.  
  78.    The MD5 algorithm is being placed in the public domain for review and
  79.    possible adoption as a standard.
  80.  
  81.    A version of this document including the C source code in the
  82.    appendix is available by FTP from RSA.COM in the file "pub/md5.doc".
  83.  
  84.    This document may be referred to, unofficially, as Internet draft
  85.    [MD5-A].
  86.  
  87.    For OSI-based applications, MD5's object identifier is
  88.  
  89.    md5 OBJECT IDENTIFIER ::=
  90.      {iso(1) member-body(2) US(840) rsadsi(113549) digestAlgorithm(2) 5}
  91.  
  92.    In the X.509 type AlgorithmIdentifier [3], the parameters for MD5
  93.    should have type NULL.
  94.  
  95.  
  96. 2. Terminology and Notation
  97.  
  98.    In this document a "word" is a 32-bit quantity and a "byte" is an
  99.    eight-bit quantity. A sequence of bits can be interpreted in a
  100.    natural manner as a sequence of bytes, where each consecutive group
  101.    of eight bits is interpreted as a byte with the high-order (most
  102.    significant) bit of each byte listed first. Similarly, a sequence of
  103.    bytes can be interpreted as a sequence of 32-bit words, where each
  104.    consecutive group of four bytes is interpreted as a word with the
  105.    low-order (least significant) byte given first.
  106.  
  107.    Let x_i denote "x sub i". If the subscript is an expression, we
  108.    surround it in braces, as in x_{i+1}. Similarly, we use ^ for
  109.    superscripts (exponentiation), so that x^i denotes x to the i-th
  110.    power.
  111.  
  112.    Let the symbol "+" denote addition of words (i.e., modulo-2^32
  113.    addition). Let X <<< s denote the 32-bit value obtained by circularly
  114.    shifting (rotating) X left by s bit positions. Let not(X) denote the
  115.  
  116.  
  117. Rivest and Dusse                                                [Page 2]
  118. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  119.  
  120.  
  121.    bit-wise complement of X, and let X v Y denote the bit-wise OR of X
  122.    and Y. Let X xor Y denote the bit-wise XOR of X and Y, and let XY
  123.    denote the bit-wise AND of X and Y.
  124.  
  125.  
  126. 3. MD5 Algorithm Description
  127.  
  128.    We begin by supposing that we have a b-bit message as input, and that
  129.    we wish to find its message digest. Here b is an arbitrary
  130.    nonnegative integer; b may be zero, it need not be a multiple of
  131.    eight, and it may be arbitrarily large. We imagine the bits of the
  132.    message written down as follows:
  133.  
  134.                             m_0 m_1 ... m_{b-1}
  135.  
  136.    The following five steps are performed to compute the message digest
  137.    of the message.
  138.  
  139.  
  140. 3.1 Step 1. Append Padding Bits
  141.  
  142.    The message is "padded" (extended) so that its length (in bits) is
  143.    congruent to 448, modulo 512. That is, the message is extended so
  144.    that it is just 64 bits shy of being a multiple of 512 bits long.
  145.    Padding is always performed, even if the length of the message is
  146.    already congruent to 448, modulo 512 (in which case 512 bits of
  147.    padding are added).
  148.  
  149.    Padding is performed as follows: a single "1" bit is appended to the
  150.    message, and then enough zero bits are appended so that the length in
  151.    bits of the padded message becomes congruent to 448, modulo 512.
  152.  
  153.  
  154. 3.2 Step 2. Append Length
  155.  
  156.    A 64-bit representation of b (the length of the message before the
  157.    padding bits were added) is appended to the result of the previous
  158.    step. In the unlikely event that b is greater than 2^64, then only
  159.    the low-order 64 bits of b are used. (These bits are appended as two
  160.    32-bit words and appended low-order word first in accordance with the
  161.    previous conventions.)
  162.  
  163.    At this point the resulting message (after padding with bits and with
  164.    b) has a length that is an exact multiple of 512 bits. Equivalently,
  165.    this message has a length that is an exact multiple of 16 (32-bit)
  166.    words. Let M[0 ... N-1] denote the words of the resulting message,
  167.    where N is a multiple of 16.
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. Rivest and Dusse                                                [Page 3]
  176. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  177.  
  178.  
  179. 3.3 Step 3. Initialize MD Buffer
  180.  
  181.    A four-word buffer (A,B,C,D) is used to compute the message digest.
  182.    Here each of A, B, C, D is a 32-bit register. These registers are
  183.    initialized to the following values in hexadecimal, low-order bytes
  184.    first):
  185.  
  186.                             word A: 01 23 45 67
  187.                             word B: 89 ab cd ef
  188.                             word C: fe dc ba 98
  189.                             word D: 76 54 32 10
  190.  
  191.  
  192. 3.4 Step 4. Process Message in 16-Word Blocks
  193.  
  194.    We first define four auxiliary functions that each take as input
  195.    three 32-bit words and produce as output one 32-bit word.
  196.  
  197.                          F(X,Y,Z) = XY v not(X) Z
  198.                          G(X,Y,Z) = XZ v Y not(Z)
  199.                          H(X,Y,Z) = X xor Y xor Z
  200.                        I(X,Y,Z) = Y xor (X v not(Z))
  201.  
  202.    In each bit position F acts as a conditional: if X then Y else Z.
  203.    (The function F could have been defined using + instead of v since XY
  204.    and not(X)Z will never have 1's in the same bit position.) It is
  205.    interesting to note that if the bits of X, Y, and Z are independent
  206.    and unbiased, the each bit of F(X,Y,Z) will be independent and
  207.    unbiased.
  208.  
  209.    The functions G, H, and I are similar to the function F, in that they
  210.    act in "bitwise parallel" to produce their output from the bits of X,
  211.    Y, and Z, in such a manner that if the corresponding bits of X, Y,
  212.    and Z are independent and unbiased, then each bit of G(X,Y,Z),
  213.    H(X,Y,Z), and I(X,Y,Z) will be independent and unbiased. Note that
  214.    the function H is the bit-wise "xor" or "parity" function of its
  215.    inputs.
  216.  
  217.    Do the following:
  218.  
  219.    /* Process each 16-word block. */
  220.    For i = 0 to N/16-1 do
  221.  
  222.        /* Copy block i into X. */
  223.        For j = 0 to 15 do
  224.            Set X[j] to M[i*16+j].
  225.        end /* of loop on j */
  226.  
  227.        /* Save A as AA, B as BB, C as CC, and D as DD. */
  228.        AA = A
  229.        BB = B
  230.        CC = C
  231.  
  232.  
  233. Rivest and Dusse                                                [Page 4]
  234. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  235.  
  236.  
  237.        DD = D
  238.  
  239.        /* Round 1. */
  240.        /* Let FF(a,b,c,d,X[k],s,t) denote the operation
  241.            a = b + ((a + F(b,c,d) + X[k] + t) <<< s). */
  242.        /* Here the additive constants t are chosen as follows:
  243.           In step i, the additive constant is the integer part of
  244.           4294967296 times abs(sin(i)), where i is in radians. */
  245.        /* Let S11 = 7, S12 = 12, S13 = 17, and S14 = 22. */
  246.        /* Do the following 16 operations. */
  247.        FF (a, b, c, d, X[ 0], S11, 3614090360); /* Step 1 */
  248.        FF (d, a, b, c, X[ 1], S12, 3905402710); /* 2 */
  249.        FF (c, d, a, b, X[ 2], S13,  606105819); /* 3 */
  250.        FF (b, c, d, a, X[ 3], S14, 3250441966); /* 4 */
  251.        FF (a, b, c, d, X[ 4], S11, 4118548399); /* 5 */
  252.        FF (d, a, b, c, X[ 5], S12, 1200080426); /* 6 */
  253.        FF (c, d, a, b, X[ 6], S13, 2821735955); /* 7 */
  254.        FF (b, c, d, a, X[ 7], S14, 4249261313); /* 8 */
  255.        FF (a, b, c, d, X[ 8], S11, 1770035416); /* 9 */
  256.        FF (d, a, b, c, X[ 9], S12, 2336552879); /* 10 */
  257.        FF (c, d, a, b, X[10], S13, 4294925233); /* 11 */
  258.        FF (b, c, d, a, X[11], S14, 2304563134); /* 12 */
  259.        FF (a, b, c, d, X[12], S11, 1804603682); /* 13 */
  260.        FF (d, a, b, c, X[13], S12, 4254626195); /* 14 */
  261.        FF (c, d, a, b, X[14], S13, 2792965006); /* 15 */
  262.        FF (b, c, d, a, X[15], S14, 1236535329); /* 16 */
  263.  
  264.        /* Round 2. */
  265.        /* Let GG(a,b,c,d,X[k],s,t) denote the operation
  266.            a = b + ((a + G(b,c,d) + X[k] + t) <<< s). */
  267.        /* Let S21 = 5, S22 = 9, S23 = 14, and S24 = 20. */
  268.  
  269.        /* Do the following 16 operations. */
  270.        GG (a, b, c, d, X[ 1], S21, 4129170786); /* 17 */
  271.        GG (d, a, b, c, X[ 6], S22, 3225465664); /* 18 */
  272.        GG (c, d, a, b, X[11], S23,  643717713); /* 19 */
  273.        GG (b, c, d, a, X[ 0], S24, 3921069994); /* 20 */
  274.        GG (a, b, c, d, X[ 5], S21, 3593408605); /* 21 */
  275.        GG (d, a, b, c, X[10], S22,   38016083); /* 22 */
  276.        GG (c, d, a, b, X[15], S23, 3634488961); /* 23 */
  277.        GG (b, c, d, a, X[ 4], S24, 3889429448); /* 24 */
  278.        GG (a, b, c, d, X[ 9], S21,  568446438); /* 25 */
  279.        GG (d, a, b, c, X[14], S22, 3275163606); /* 26 */
  280.        GG (c, d, a, b, X[ 3], S23, 4107603335); /* 27 */
  281.        GG (b, c, d, a, X[ 8], S24, 1163531501); /* 28 */
  282.        GG (a, b, c, d, X[13], S21, 2850285829); /* 29 */
  283.        GG (d, a, b, c, X[ 2], S22, 4243563512); /* 30 */
  284.        GG (c, d, a, b, X[ 7], S23, 1735328473); /* 31 */
  285.        GG (b, c, d, a, X[12], S24, 2368359562); /* 32 */
  286.  
  287.        /* Round 3. */
  288.        /* Let HH(a,b,c,d,X[k],s,t) denote the operation
  289.  
  290.  
  291. Rivest and Dusse                                                [Page 5]
  292. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  293.  
  294.  
  295.            a = b + ((a + H(b,c,d) + X[k] + t) <<< s). */
  296.        /* Let S31 = 4, S32 = 11, S33 = 16, and S34 = 23. */
  297.  
  298.        /* Do the following 16 operations. */
  299.        HH (a, b, c, d, X[ 5], S31, 4294588738); /* 33 */
  300.        HH (d, a, b, c, X[ 8], S32, 2272392833); /* 34 */
  301.        HH (c, d, a, b, X[11], S33, 1839030562); /* 35 */
  302.        HH (b, c, d, a, X[14], S34, 4259657740); /* 36 */
  303.        HH (a, b, c, d, X[ 1], S31, 2763975236); /* 37 */
  304.        HH (d, a, b, c, X[ 4], S32, 1272893353); /* 38 */
  305.        HH (c, d, a, b, X[ 7], S33, 4139469664); /* 39 */
  306.        HH (b, c, d, a, X[10], S34, 3200236656); /* 40 */
  307.        HH (a, b, c, d, X[13], S31,  681279174); /* 41 */
  308.        HH (d, a, b, c, X[ 0], S32, 3936430074); /* 42 */
  309.        HH (c, d, a, b, X[ 3], S33, 3572445317); /* 43 */
  310.        HH (b, c, d, a, X[ 6], S34,   76029189); /* 44 */
  311.        HH (a, b, c, d, X[ 9], S31, 3654602809); /* 45 */
  312.        HH (d, a, b, c, X[12], S32, 3873151461); /* 46 */
  313.        HH (c, d, a, b, X[15], S33,  530742520); /* 47 */
  314.        HH (b, c, d, a, X[ 2], S34, 3299628645); /* 48 */
  315.  
  316.        /* Round 4. */
  317.        /* Let II(a,b,c,d,X[k],s,t) denote the operation
  318.            a = b + ((a + I(b,c,d) + X[k] + t) <<< s). */
  319.        /* Let S41 = 6, S42 = 10, S43 = 15, and S44 = 21. */
  320.  
  321.        /* Do the following 16 operations. */
  322.        II (a, b, c, d, X[ 0], S41, 4096336452); /* 49 */
  323.        II (d, a, b, c, X[ 7], S42, 1126891415); /* 50 */
  324.        II (c, d, a, b, X[14], S43, 2878612391); /* 51 */
  325.        II (b, c, d, a, X[ 5], S44, 4237533241); /* 52 */
  326.        II (a, b, c, d, X[12], S41, 1700485571); /* 53 */
  327.        II (d, a, b, c, X[ 3], S42, 2399980690); /* 54 */
  328.        II (c, d, a, b, X[10], S43, 4293915773); /* 55 */
  329.        II (b, c, d, a, X[ 1], S44, 2240044497); /* 56 */
  330.        II (a, b, c, d, X[ 8], S41, 1873313359); /* 57 */
  331.        II (d, a, b, c, X[15], S42, 4264355552); /* 58 */
  332.        II (c, d, a, b, X[ 6], S43, 2734768916); /* 59 */
  333.        II (b, c, d, a, X[13], S44, 1309151649); /* 60 */
  334.        II (a, b, c, d, X[ 4], S41, 4149444226); /* 61 */
  335.        II (d, a, b, c, X[11], S42, 3174756917); /* 62 */
  336.        II (c, d, a, b, X[ 2], S43,  718787259); /* 63 */
  337.        II (b, c, d, a, X[ 9], S44, 3951481745); /* 64 */
  338.  
  339.        /* Then perform the following additions. (That is, increment each
  340.           of the four registers by the value it had before this block
  341.           was started.) */
  342.        A = A + AA
  343.        B = B + BB
  344.        C = C + CC
  345.        D = D + DD
  346.  
  347.  
  348.  
  349. Rivest and Dusse                                                [Page 6]
  350. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  351.  
  352.  
  353.  
  354.    end /* of loop on i */
  355.  
  356.  
  357. 3.5 Step 5. Output
  358.  
  359.    The message digest produced as output is A, B, C, D. That is, we
  360.    begin with the low-order byte of A, and end with the high-order byte
  361.    of D.
  362.  
  363.    This completes the description of MD5. A reference implementation in
  364.    C is given in the Appendix.
  365.  
  366.  
  367. 4. Summary
  368.  
  369.    The MD5 message-digest algorithm is simple to implement, and provides
  370.    a "fingerprint" or message digest of a message of arbitrary length.
  371.    It is conjectured that the difficulty of coming up with two messages
  372.    having the same message digest is on the order of 2^64 operations,
  373.    and that the difficulty of coming up with any message having a given
  374.    message digest is on the order of 2^128 operations. The MD5 algorithm
  375.    has been carefully scrutinized for weaknesses. It is, however, a
  376.    relatively new algorithm and further security analysis is of course
  377.    justified, as is the case with any new proposal of this sort.
  378.  
  379.  
  380. 5. Summary of Differences Between MD4 and MD5
  381.  
  382.    The following are the differences between MD4 and MD5:
  383.  
  384.      --   A fourth round has been added.
  385.  
  386.      --   Each step now has a unique additive constant.
  387.  
  388.      --   The function g in round 2 was changed from (XY v XZ v YZ)
  389.           to (XZ v Y not(Z)) to make g less symmetric.
  390.  
  391.      --   Each step now adds in the result of the previous step.
  392.           This promotes a faster "avalanche effect".
  393.  
  394.      --   The order in which input words are accessed in rounds 2
  395.           and 3 is changed, to make these patterns less like each
  396.           other.
  397.  
  398.      --   The shift amounts in each round have been approximately
  399.           optimized, to yield a faster "avalanche effect". The
  400.           shifts in different rounds are distinct.
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407. Rivest and Dusse                                                [Page 7]
  408. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  409.  
  410.  
  411. 6. Security Considerations
  412.  
  413.    The level of security discussed in this memo is considered to be
  414.    sufficient for implementing very high security hybrid digital-
  415.    signature schemes based on MD5 and a public-key cryptosystem.
  416.  
  417.  
  418. References
  419.  
  420.      [1]  Rivest, R.L., The MD4 Message Digest Algorithm (RFC 1186),
  421.           October 1990.
  422.  
  423.      [2]  Rivest, R.L., The MD4 message digest algorithm, presented at
  424.           CRYPTO '90 (Santa Barbara, CA, August 11-15, 1990).
  425.  
  426.      [3]  CCITT, The Directory---Authentication Framework
  427.           (Recommendation X.509), 1988.
  428.  
  429.  
  430.  
  431.  
  432. Authors' Addresses
  433.  
  434.    Ronald L. Rivest
  435.    Massachusetts Institute of Technology
  436.    Laboratory for Computer Science
  437.    NE43-324
  438.    545 Technology Square
  439.    Cambridge, MA  02139-1986
  440.    Phone: (617) 253-5880
  441.    EMail: rivest@theory.lcs.mit.edu
  442.  
  443.    Steve Dusse
  444.    RSA Data Security, Inc.
  445.    10 Twin Dolphin Drive
  446.    Redwood City, CA  94065
  447.    Phone: (415) 595-8782
  448.    EMail: dusse@rsa.com
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465. Rivest and Dusse                                                [Page 8]
  466. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  467.  
  468.  
  469.  
  470. APPENDIX - Reference Implementation
  471.  
  472.    This appendix contains the following files:
  473.  
  474.      md5.h -- header file for implementation of MD5
  475.  
  476.      md5.c -- the source code for MD5 routines
  477.  
  478.      md5driver.c -- sample test routines
  479.  
  480.      session -- sample results of running md5driver
  481.  
  482.    It is not difficult to improve this implementation on particular
  483.    platforms, an exercise left to the reader. Following are some
  484.    suggestions:
  485.  
  486.      1.   Change MD5Update so that the context is not used at all
  487.           if it is empty (mdi == 0) and 64 or more bytes remain
  488.           (inLen >= 64). In other words, call Transform with inBuf
  489.           in this case. (This requires that byte ordering is
  490.           correct in inBuf.)
  491.  
  492.      2.   Implement a procedure MD5UpdateLong modeled after
  493.           MD5Update where inBuf is UINT4 * instead of unsigned char
  494.           *. MD5UpdateLong would call Transform directly with 16-
  495.           word blocks from inBuf. Call this instead of MD5Update in
  496.           general. This works well if you have an I/O procedure
  497.           that can read long words from a file.
  498.  
  499.      3.   On "little-endian" platforms where the lowest-address
  500.           byte in a long word is the least significant (and there
  501.           are no alignment restrictions), change MD5Update to call
  502.           Transform directly with 64-byte blocks from inBuf
  503.           (typecast to a UINT4 *).
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523. Rivest and Dusse                                                [Page 9]
  524. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  525.  
  526.  
  527. /*
  528.  ***********************************************************************
  529.  ** md5.h -- header file for implementation of MD5                    **
  530.  ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
  531.  ** Created: 2/17/90 RLR                                              **
  532.  ** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version               **
  533.  ** Revised (for MD5): RLR 4/27/91                                    **
  534.  **   -- G modified to have y&~z instead of y&z                       **
  535.  **   -- FF, GG, HH modified to add in last register done             **
  536.  **   -- Access pattern: round 2 works mod 5, round 3 works mod 3     **
  537.  **   -- distinct additive constant for each step                     **
  538.  **   -- round 4 added, working mod 7                                 **
  539.  ***********************************************************************
  540.  */
  541.  
  542. /*
  543.  ***********************************************************************
  544.  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.  **
  545.  **                                                                   **
  546.  ** License to copy and use this software is granted provided that    **
  547.  ** it is identified as the "RSA Data Security, Inc. MD5 Message-     **
  548.  ** Digest Algorithm" in all material mentioning or referencing this  **
  549.  ** software or this function.                                        **
  550.  **                                                                   **
  551.  ** License is also granted to make and use derivative works          **
  552.  ** provided that such works are identified as "derived from the RSA  **
  553.  ** Data Security, Inc. MD5 Message-Digest Algorithm" in all          **
  554.  ** material mentioning or referencing the derived work.              **
  555.  **                                                                   **
  556.  ** RSA Data Security, Inc. makes no representations concerning       **
  557.  ** either the merchantability of this software or the suitability    **
  558.  ** of this software for any particular purpose.  It is provided "as  **
  559.  ** is" without express or implied warranty of any kind.              **
  560.  **                                                                   **
  561.  ** These notices must be retained in any copies of any part of this  **
  562.  ** documentation and/or software.                                    **
  563.  ***********************************************************************
  564.  */
  565.  
  566. /* typedef a 32-bit type */
  567. typedef unsigned long int UINT4;
  568.  
  569. /* Data structure for MD5 (Message-Digest) computation */
  570. typedef struct {
  571.   UINT4 i[2];                   /* number of _bits_ handled mod 2^64 */
  572.   UINT4 buf[4];                                    /* scratch buffer */
  573.   unsigned char in[64];                              /* input buffer */
  574.   unsigned char digest[16];     /* actual digest after MD5Final call */
  575. } MD5_CTX;
  576.  
  577. void MD5Init ();
  578. void MD5Update ();
  579.  
  580.  
  581. Rivest and Dusse                                               [Page 10]
  582. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  583.  
  584.  
  585. void MD5Final ();
  586.  
  587. /*
  588.  ***********************************************************************
  589.  ** End of md5.h                                                      **
  590.  ******************************** (cut) ********************************
  591.  */
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639. Rivest and Dusse                                               [Page 11]
  640. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  641.  
  642.  
  643. /*
  644.  ***********************************************************************
  645.  ** md5.c -- the source code for MD5 routines                         **
  646.  ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
  647.  ** Created: 2/17/90 RLR                                              **
  648.  ** Revised: 1/91 SRD,AJ,BSK,JT Reference C ver., 7/10 constant corr. **
  649.  ***********************************************************************
  650.  */
  651.  
  652. /*
  653.  ***********************************************************************
  654.  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.  **
  655.  **                                                                   **
  656.  ** License to copy and use this software is granted provided that    **
  657.  ** it is identified as the "RSA Data Security, Inc. MD5 Message-     **
  658.  ** Digest Algorithm" in all material mentioning or referencing this  **
  659.  ** software or this function.                                        **
  660.  **                                                                   **
  661.  ** License is also granted to make and use derivative works          **
  662.  ** provided that such works are identified as "derived from the RSA  **
  663.  ** Data Security, Inc. MD5 Message-Digest Algorithm" in all          **
  664.  ** material mentioning or referencing the derived work.              **
  665.  **                                                                   **
  666.  ** RSA Data Security, Inc. makes no representations concerning       **
  667.  ** either the merchantability of this software or the suitability    **
  668.  ** of this software for any particular purpose.  It is provided "as  **
  669.  ** is" without express or implied warranty of any kind.              **
  670.  **                                                                   **
  671.  ** These notices must be retained in any copies of any part of this  **
  672.  ** documentation and/or software.                                    **
  673.  ***********************************************************************
  674.  */
  675.  
  676. #include "md5.h"
  677.  
  678. /*
  679.  ***********************************************************************
  680.  **  Message-digest routines:                                         **
  681.  **  To form the message digest for a message M                       **
  682.  **    (1) Initialize a context buffer mdContext using MD5Init        **
  683.  **    (2) Call MD5Update on mdContext and M                          **
  684.  **    (3) Call MD5Final on mdContext                                 **
  685.  **  The message digest is now in mdContext->digest[0...15]           **
  686.  ***********************************************************************
  687.  */
  688.  
  689. /* forward declaration */
  690. static void Transform ();
  691.  
  692. static unsigned char PADDING[64] = {
  693.   0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  694.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  695.  
  696.  
  697. Rivest and Dusse                                               [Page 12]
  698. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  699.  
  700.  
  701.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  702.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  703.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  704.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  705.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  706.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  707. };
  708.  
  709. /* F, G, H and I are basic MD5 functions */
  710. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  711. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  712. #define H(x, y, z) ((x) ^ (y) ^ (z))
  713. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  714.  
  715. /* ROTATE_LEFT rotates x left n bits */
  716. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  717.  
  718. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
  719. /* Rotation is separate from addition to prevent recomputation */
  720. #define FF(a, b, c, d, x, s, ac) \
  721.   {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  722.    (a) = ROTATE_LEFT ((a), (s)); \
  723.    (a) += (b); \
  724.   }
  725. #define GG(a, b, c, d, x, s, ac) \
  726.   {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  727.    (a) = ROTATE_LEFT ((a), (s)); \
  728.    (a) += (b); \
  729.   }
  730. #define HH(a, b, c, d, x, s, ac) \
  731.   {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  732.    (a) = ROTATE_LEFT ((a), (s)); \
  733.    (a) += (b); \
  734.   }
  735. #define II(a, b, c, d, x, s, ac) \
  736.   {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  737.    (a) = ROTATE_LEFT ((a), (s)); \
  738.    (a) += (b); \
  739.   }
  740.  
  741. /* The routine MD5Init initializes the message-digest context
  742.    mdContext. All fields are set to zero.
  743.  */
  744. void MD5Init (mdContext)
  745. MD5_CTX *mdContext;
  746. {
  747.   mdContext->i[0] = mdContext->i[1] = (UINT4)0;
  748.  
  749.   /* Load magic initialization constants.
  750.    */
  751.   mdContext->buf[0] = (UINT4)0x67452301;
  752.   mdContext->buf[1] = (UINT4)0xefcdab89;
  753.  
  754.  
  755. Rivest and Dusse                                               [Page 13]
  756. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  757.  
  758.  
  759.   mdContext->buf[2] = (UINT4)0x98badcfe;
  760.   mdContext->buf[3] = (UINT4)0x10325476;
  761. }
  762.  
  763. /* The routine MD5Update updates the message-digest context to
  764.    account for the presence of each of the characters inBuf[0..inLen-1]
  765.    in the message whose digest is being computed.
  766.  */
  767. void MD5Update (mdContext, inBuf, inLen)
  768. MD5_CTX *mdContext;
  769. unsigned char *inBuf;
  770. unsigned int inLen;
  771. {
  772.   UINT4 in[16];
  773.   int mdi;
  774.   unsigned int i, ii;
  775.  
  776.   /* compute number of bytes mod 64 */
  777.   mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
  778.  
  779.   /* update number of bits */
  780.   if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
  781.     mdContext->i[1]++;
  782.   mdContext->i[0] += ((UINT4)inLen << 3);
  783.   mdContext->i[1] += ((UINT4)inLen >> 29);
  784.  
  785.   while (inLen--) {
  786.     /* add new character to buffer, increment mdi */
  787.     mdContext->in[mdi++] = *inBuf++;
  788.  
  789.     /* transform if necessary */
  790.     if (mdi == 0x40) {
  791.       for (i = 0, ii = 0; i < 16; i++, ii += 4)
  792.         in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
  793.                 (((UINT4)mdContext->in[ii+2]) << 16) |
  794.                 (((UINT4)mdContext->in[ii+1]) << 8) |
  795.                 ((UINT4)mdContext->in[ii]);
  796.       Transform (mdContext->buf, in);
  797.       mdi = 0;
  798.     }
  799.   }
  800. }
  801.  
  802. /* The routine MD5Final terminates the message-digest computation and
  803.    ends with the desired message digest in mdContext->digest[0...15].
  804.  */
  805. void MD5Final (mdContext)
  806. MD5_CTX *mdContext;
  807. {
  808.   UINT4 in[16];
  809.   int mdi;
  810.   unsigned int i, ii;
  811.  
  812.  
  813. Rivest and Dusse                                               [Page 14]
  814. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  815.  
  816.  
  817.   unsigned int padLen;
  818.  
  819.   /* save number of bits */
  820.   in[14] = mdContext->i[0];
  821.   in[15] = mdContext->i[1];
  822.  
  823.   /* compute number of bytes mod 64 */
  824.   mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
  825.  
  826.   /* pad out to 56 mod 64 */
  827.   padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
  828.   MD5Update (mdContext, PADDING, padLen);
  829.  
  830.   /* append length in bits and transform */
  831.   for (i = 0, ii = 0; i < 14; i++, ii += 4)
  832.     in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
  833.             (((UINT4)mdContext->in[ii+2]) << 16) |
  834.             (((UINT4)mdContext->in[ii+1]) << 8) |
  835.             ((UINT4)mdContext->in[ii]);
  836.   Transform (mdContext->buf, in);
  837.  
  838.   /* store buffer in digest */
  839.   for (i = 0, ii = 0; i < 4; i++, ii += 4) {
  840.     mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
  841.     mdContext->digest[ii+1] =
  842.       (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
  843.     mdContext->digest[ii+2] =
  844.       (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
  845.     mdContext->digest[ii+3] =
  846.       (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
  847.   }
  848. }
  849.  
  850. /* Basic MD5 step. Transforms buf based on in.
  851.  */
  852. static void Transform (buf, in)
  853. UINT4 *buf;
  854. UINT4 *in;
  855. {
  856.   UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
  857.  
  858.   /* Round 1 */
  859. #define S11 7
  860. #define S12 12
  861. #define S13 17
  862. #define S14 22
  863.   FF ( a, b, c, d, in[ 0], S11, 3614090360); /* 1 */
  864.   FF ( d, a, b, c, in[ 1], S12, 3905402710); /* 2 */
  865.   FF ( c, d, a, b, in[ 2], S13,  606105819); /* 3 */
  866.   FF ( b, c, d, a, in[ 3], S14, 3250441966); /* 4 */
  867.   FF ( a, b, c, d, in[ 4], S11, 4118548399); /* 5 */
  868.   FF ( d, a, b, c, in[ 5], S12, 1200080426); /* 6 */
  869.  
  870.  
  871. Rivest and Dusse                                               [Page 15]
  872. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  873.  
  874.  
  875.   FF ( c, d, a, b, in[ 6], S13, 2821735955); /* 7 */
  876.   FF ( b, c, d, a, in[ 7], S14, 4249261313); /* 8 */
  877.   FF ( a, b, c, d, in[ 8], S11, 1770035416); /* 9 */
  878.   FF ( d, a, b, c, in[ 9], S12, 2336552879); /* 10 */
  879.   FF ( c, d, a, b, in[10], S13, 4294925233); /* 11 */
  880.   FF ( b, c, d, a, in[11], S14, 2304563134); /* 12 */
  881.   FF ( a, b, c, d, in[12], S11, 1804603682); /* 13 */
  882.   FF ( d, a, b, c, in[13], S12, 4254626195); /* 14 */
  883.   FF ( c, d, a, b, in[14], S13, 2792965006); /* 15 */
  884.   FF ( b, c, d, a, in[15], S14, 1236535329); /* 16 */
  885.  
  886.   /* Round 2 */
  887. #define S21 5
  888. #define S22 9
  889. #define S23 14
  890. #define S24 20
  891.   GG ( a, b, c, d, in[ 1], S21, 4129170786); /* 17 */
  892.   GG ( d, a, b, c, in[ 6], S22, 3225465664); /* 18 */
  893.   GG ( c, d, a, b, in[11], S23,  643717713); /* 19 */
  894.   GG ( b, c, d, a, in[ 0], S24, 3921069994); /* 20 */
  895.   GG ( a, b, c, d, in[ 5], S21, 3593408605); /* 21 */
  896.   GG ( d, a, b, c, in[10], S22,   38016083); /* 22 */
  897.   GG ( c, d, a, b, in[15], S23, 3634488961); /* 23 */
  898.   GG ( b, c, d, a, in[ 4], S24, 3889429448); /* 24 */
  899.   GG ( a, b, c, d, in[ 9], S21,  568446438); /* 25 */
  900.   GG ( d, a, b, c, in[14], S22, 3275163606); /* 26 */
  901.   GG ( c, d, a, b, in[ 3], S23, 4107603335); /* 27 */
  902.   GG ( b, c, d, a, in[ 8], S24, 1163531501); /* 28 */
  903.   GG ( a, b, c, d, in[13], S21, 2850285829); /* 29 */
  904.   GG ( d, a, b, c, in[ 2], S22, 4243563512); /* 30 */
  905.   GG ( c, d, a, b, in[ 7], S23, 1735328473); /* 31 */
  906.   GG ( b, c, d, a, in[12], S24, 2368359562); /* 32 */
  907.  
  908.   /* Round 3 */
  909. #define S31 4
  910. #define S32 11
  911. #define S33 16
  912. #define S34 23
  913.   HH ( a, b, c, d, in[ 5], S31, 4294588738); /* 33 */
  914.   HH ( d, a, b, c, in[ 8], S32, 2272392833); /* 34 */
  915.   HH ( c, d, a, b, in[11], S33, 1839030562); /* 35 */
  916.   HH ( b, c, d, a, in[14], S34, 4259657740); /* 36 */
  917.   HH ( a, b, c, d, in[ 1], S31, 2763975236); /* 37 */
  918.   HH ( d, a, b, c, in[ 4], S32, 1272893353); /* 38 */
  919.   HH ( c, d, a, b, in[ 7], S33, 4139469664); /* 39 */
  920.   HH ( b, c, d, a, in[10], S34, 3200236656); /* 40 */
  921.   HH ( a, b, c, d, in[13], S31,  681279174); /* 41 */
  922.   HH ( d, a, b, c, in[ 0], S32, 3936430074); /* 42 */
  923.   HH ( c, d, a, b, in[ 3], S33, 3572445317); /* 43 */
  924.   HH ( b, c, d, a, in[ 6], S34,   76029189); /* 44 */
  925.   HH ( a, b, c, d, in[ 9], S31, 3654602809); /* 45 */
  926.   HH ( d, a, b, c, in[12], S32, 3873151461); /* 46 */
  927.  
  928.  
  929. Rivest and Dusse                                               [Page 16]
  930. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  931.  
  932.  
  933.   HH ( c, d, a, b, in[15], S33,  530742520); /* 47 */
  934.   HH ( b, c, d, a, in[ 2], S34, 3299628645); /* 48 */
  935.  
  936.   /* Round 4 */
  937. #define S41 6
  938. #define S42 10
  939. #define S43 15
  940. #define S44 21
  941.   II ( a, b, c, d, in[ 0], S41, 4096336452); /* 49 */
  942.   II ( d, a, b, c, in[ 7], S42, 1126891415); /* 50 */
  943.   II ( c, d, a, b, in[14], S43, 2878612391); /* 51 */
  944.   II ( b, c, d, a, in[ 5], S44, 4237533241); /* 52 */
  945.   II ( a, b, c, d, in[12], S41, 1700485571); /* 53 */
  946.   II ( d, a, b, c, in[ 3], S42, 2399980690); /* 54 */
  947.   II ( c, d, a, b, in[10], S43, 4293915773); /* 55 */
  948.   II ( b, c, d, a, in[ 1], S44, 2240044497); /* 56 */
  949.   II ( a, b, c, d, in[ 8], S41, 1873313359); /* 57 */
  950.   II ( d, a, b, c, in[15], S42, 4264355552); /* 58 */
  951.   II ( c, d, a, b, in[ 6], S43, 2734768916); /* 59 */
  952.   II ( b, c, d, a, in[13], S44, 1309151649); /* 60 */
  953.   II ( a, b, c, d, in[ 4], S41, 4149444226); /* 61 */
  954.   II ( d, a, b, c, in[11], S42, 3174756917); /* 62 */
  955.   II ( c, d, a, b, in[ 2], S43,  718787259); /* 63 */
  956.   II ( b, c, d, a, in[ 9], S44, 3951481745); /* 64 */
  957.  
  958.   buf[0] += a;
  959.   buf[1] += b;
  960.   buf[2] += c;
  961.   buf[3] += d;
  962. }
  963.  
  964. /*
  965.  ***********************************************************************
  966.  ** End of md5.c                                                      **
  967.  ******************************** (cut) ********************************
  968.  */
  969.  
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987. Rivest and Dusse                                               [Page 17]
  988. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  989.  
  990.  
  991. /*
  992.  ***********************************************************************
  993.  ** md5driver.c -- sample test routines                               **
  994.  ** RSA Data Security, Inc. MD5 Message-Digest Algorithm              **
  995.  ** Created: 2/16/90 RLR                                              **
  996.  ** Updated: 1/91 SRD                                                 **
  997.  ***********************************************************************
  998.  */
  999.  
  1000. /*
  1001.  ***********************************************************************
  1002.  ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.  **
  1003.  **                                                                   **
  1004.  ** RSA Data Security, Inc. makes no representations concerning       **
  1005.  ** either the merchantability of this software or the suitability    **
  1006.  ** of this software for any particular purpose.  It is provided "as  **
  1007.  ** is" without express or implied warranty of any kind.              **
  1008.  **                                                                   **
  1009.  ** These notices must be retained in any copies of any part of this  **
  1010.  ** documentation and/or software.                                    **
  1011.  ***********************************************************************
  1012.  */
  1013.  
  1014. #include <stdio.h>
  1015. #include <sys/types.h>
  1016. #include <time.h>
  1017. #include <string.h>
  1018. #include "md5.h"
  1019.  
  1020. /* Prints message digest buffer in mdContext as 32 hexadecimal digits.
  1021.    Order is from low-order byte to high-order byte of digest.
  1022.    Each byte is printed with high-order hexadecimal digit first.
  1023.  */
  1024. static void MDPrint (mdContext)
  1025. MD5_CTX *mdContext;
  1026. {
  1027.   int i;
  1028.  
  1029.   for (i = 0; i < 16; i++)
  1030.     printf ("%02x", mdContext->digest[i]);
  1031. }
  1032.  
  1033. /* size of test block */
  1034. #define TEST_BLOCK_SIZE 1000
  1035.  
  1036. /* number of blocks to process */
  1037. #define TEST_BLOCKS 10000
  1038.  
  1039. /* number of test bytes = TEST_BLOCK_SIZE * TEST_BLOCKS */
  1040. static long TEST_BYTES = (long)TEST_BLOCK_SIZE * (long)TEST_BLOCKS;
  1041.  
  1042. /* A time trial routine, to measure the speed of MD5.
  1043.  
  1044.  
  1045. Rivest and Dusse                                               [Page 18]
  1046. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  1047.  
  1048.  
  1049.    Measures wall time required to digest TEST_BLOCKS * TEST_BLOCK_SIZE
  1050.    characters.
  1051.  */
  1052. static void MDTimeTrial ()
  1053. {
  1054.   MD5_CTX mdContext;
  1055.   time_t endTime, startTime;
  1056.   unsigned char data[TEST_BLOCK_SIZE];
  1057.   unsigned int i;
  1058.  
  1059.   /* initialize test data */
  1060.   for (i = 0; i < TEST_BLOCK_SIZE; i++)
  1061.     data[i] = (unsigned char)(i & 0xFF);
  1062.  
  1063.   /* start timer */
  1064.   printf ("MD5 time trial. Processing %ld characters...\n", TEST_BYTES);
  1065.   time (&startTime);
  1066.  
  1067.   /* digest data in TEST_BLOCK_SIZE byte blocks */
  1068.   MD5Init (&mdContext);
  1069.   for (i = TEST_BLOCKS; i > 0; i--)
  1070.     MD5Update (&mdContext, data, TEST_BLOCK_SIZE);
  1071.   MD5Final (&mdContext);
  1072.  
  1073.   /* stop timer, get time difference */
  1074.   time (&endTime);
  1075.   MDPrint (&mdContext);
  1076.   printf (" is digest of test input.\n");
  1077.   printf
  1078.     ("Seconds to process test input: %ld\n", (long)(endTime-startTime));
  1079.   printf
  1080.     ("Characters processed per second: %ld\n",
  1081.      TEST_BYTES/(endTime-startTime));
  1082. }
  1083.  
  1084. /* Computes the message digest for string inString.
  1085.    Prints out message digest, a space, the string (in quotes) and a
  1086.    carriage return.
  1087.  */
  1088. static void MDString (inString)
  1089. char *inString;
  1090. {
  1091.   MD5_CTX mdContext;
  1092.   unsigned int len = strlen (inString);
  1093.  
  1094.   MD5Init (&mdContext);
  1095.   MD5Update (&mdContext, inString, len);
  1096.   MD5Final (&mdContext);
  1097.   MDPrint (&mdContext);
  1098.   printf (" \"%s\"\n", inString);
  1099. }
  1100.  
  1101.  
  1102.  
  1103. Rivest and Dusse                                               [Page 19]
  1104. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  1105.  
  1106.  
  1107. /* Computes the message digest for a specified file.
  1108.    Prints out message digest, a space, the file name, and a carriage
  1109.    return.
  1110.  */
  1111. static void MDFile (filename)
  1112. char *filename;
  1113. {
  1114.   FILE *inFile = fopen (filename, "rb");
  1115.   MD5_CTX mdContext;
  1116.   int bytes;
  1117.   unsigned char data[1024];
  1118.  
  1119.   if (inFile == NULL) {
  1120.     printf ("%s can't be opened.\n", filename);
  1121.     return;
  1122.   }
  1123.  
  1124.   MD5Init (&mdContext);
  1125.   while ((bytes = fread (data, 1, 1024, inFile)) != 0)
  1126.     MD5Update (&mdContext, data, bytes);
  1127.   MD5Final (&mdContext);
  1128.   MDPrint (&mdContext);
  1129.   printf (" %s\n", filename);
  1130.   fclose (inFile);
  1131. }
  1132.  
  1133. /* Writes the message digest of the data from stdin onto stdout,
  1134.    followed by a carriage return.
  1135.  */
  1136. static void MDFilter ()
  1137. {
  1138.   MD5_CTX mdContext;
  1139.   int bytes;
  1140.   unsigned char data[16];
  1141.  
  1142.   MD5Init (&mdContext);
  1143.   while ((bytes = fread (data, 1, 16, stdin)) != 0)
  1144.     MD5Update (&mdContext, data, bytes);
  1145.   MD5Final (&mdContext);
  1146.   MDPrint (&mdContext);
  1147.   printf ("\n");
  1148. }
  1149.  
  1150. /* Runs a standard suite of test data.
  1151.  */
  1152. static void MDTestSuite ()
  1153. {
  1154.   printf ("MD5 test suite results:\n");
  1155.   MDString ("");
  1156.   MDString ("a");
  1157.   MDString ("abc");
  1158.   MDString ("message digest");
  1159.  
  1160.  
  1161. Rivest and Dusse                                               [Page 20]
  1162. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  1163.  
  1164.  
  1165.   MDString ("abcdefghijklmnopqrstuvwxyz");
  1166.   MDString
  1167.     ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
  1168.   MDString
  1169.     ("1234567890123456789012345678901234567890\
  1170. 1234567890123456789012345678901234567890");
  1171.   /* Contents of file foo are "abc" */
  1172.   MDFile ("foo");
  1173. }
  1174.  
  1175. void main (argc, argv)
  1176. int argc;
  1177. char *argv[];
  1178. {
  1179.   int i;
  1180.  
  1181.   /* For each command line argument in turn:
  1182.   ** filename          -- prints message digest and name of file
  1183.   ** -sstring          -- prints message digest and contents of string
  1184.   ** -t                -- prints time trial statistics for 10M
  1185.                           characters
  1186.   ** -x                -- execute a standard suite of test data
  1187.   ** (no args)         -- writes messages digest of stdin onto stdout
  1188.   */
  1189.   if (argc == 1)
  1190.     MDFilter ();
  1191.   else
  1192.     for (i = 1; i < argc; i++)
  1193.       if (argv[i][0] == '-' && argv[i][1] == 's')
  1194.         MDString (argv[i] + 2);
  1195.       else if (strcmp (argv[i], "-t") == 0)
  1196.         MDTimeTrial ();
  1197.       else if (strcmp (argv[i], "-x") == 0)
  1198.         MDTestSuite ();
  1199.       else MDFile (argv[i]);
  1200. }
  1201.  
  1202. /*
  1203.  ***********************************************************************
  1204.  ** End of md5driver.c                                                **
  1205.  ******************************** (cut) ********************************
  1206.  */
  1207.  
  1208.  
  1209.  
  1210.  
  1211.  
  1212.  
  1213.  
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219. Rivest and Dusse                                               [Page 21]
  1220. INTERNET-DRAFT      The MD5 Message-Digest Algorithm        10 July 1991
  1221.  
  1222.  
  1223. ------------------------------------------------------------------------
  1224. -- Sample session output obtained by running md5driver test suite     --
  1225. ------------------------------------------------------------------------
  1226.  
  1227. MD5 test suite results:
  1228. d41d8cd98f00b204e9800998ecf8427e ""
  1229. 0cc175b9c0f1b6a831c399e269772661 "a"
  1230. 900150983cd24fb0d6963f7d28e17f72 "abc"
  1231. f96b697d7cb7938d525a2f31aaf161d0 "message digest"
  1232. c3fcd3d76192e4007dfb496cca67e13b "abcdefghijklmnopqrstuvwxyz"
  1233. d174ab98d277d9f5a5611c2c9f419d9f "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk\
  1234. lmnopqrstuvwxyz0123456789"
  1235. 57edf4a22be3c955ac49da2e2107b67a "1234567890123456789012345678901234567\
  1236. 8901234567890123456789012345678901234567890"
  1237. 900150983cd24fb0d6963f7d28e17f72 foo
  1238.  
  1239. ------------------------------------------------------------------------
  1240. -- End of sample session                                              --
  1241. -------------------------------- (cut) ---------------------------------
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.  
  1259.  
  1260.  
  1261.  
  1262.  
  1263.  
  1264.  
  1265.  
  1266.  
  1267.  
  1268.  
  1269.  
  1270.  
  1271.  
  1272.  
  1273.  
  1274.  
  1275.  
  1276.  
  1277. Rivest and Dusse                                               [Page 22]
  1278.  
  1279.